﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
using WebFabricator.org;

namespace WebFabricator.org
{
    public  static partial class Extensions
    {
        public static IEnumerable<string> SegmentsAndFragments(this Uri uri)
        {
            return new string[] { uri.DnsSafeHost }.Union(uri.Segments.Union(new String[] { uri.Fragment }).Select(s => s.TrimEnd('/'))).Where(s => !String.IsNullOrEmpty(s));
        }

        //public static string ToText(this object type)
        //{
        //    if (type is byte[])
        //        return System.Text.ASCIIEncoding.UTF8.GetString((byte[])type);
        //    else if (type is IResolve)
        //        return ToText(((IResolve)type).ResolveToEnd());
        //    else if (type is Uri)
        //        return (type as Uri).Normalize().ToString();
        //    else
        //        return type.ToString();
        //}

        //public static object ResolveToEnd(this IResolve resolver)
        //{
        //    object o = resolver;
        //    while (o is IResolve)
        //    {
        //        o = ((IResolve)o).Resolve();
        //    }
        //    return o;
        //}

        public static void Parse<T>(this IWebFabric<T> fabric, Uri absoluteUri)
        {
            var variables = HttpUtility.ParseQueryString(absoluteUri.Query);
            variables.AllKeys
                .ToList()
                .ForEach(k =>
                {
                   //fabric.RouteData.DataTokens.Add(k, variables.Get(k));
                });
        }

    }
}
